package cn.com.git.example.module.service.impl;

import cn.com.git.example.common.auth.LoginProcessDTO;
import cn.com.git.example.common.auth.LoginStatusDTO;
import cn.com.git.example.common.base.BaseCacheService;
import cn.com.git.example.module.service.AuthenticationCacheService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 鉴权缓存
 * @author zhangzhenfu
 * @Description
 * @date 2021/3/31 11:16
 */
@Slf4j
@Service
public class AuthenticationCacheServiceImpl extends BaseCacheService implements AuthenticationCacheService {

    //登录过程 key
    private static String LOGIN_PROCESS_CODE = "login_process";
    //登录状态 key
    private static String LOGIN_STATUS_CODE = "login_status";
    //登录临时码 key
    private static String LOGIN_UUID_CODE = "login_uuid";
    //登录过程 持续 30分钟
    private static long LOGIN_PROCESS_CODE_OUT_TIME = 30 * 60;
    //登录状态 持续 1 小时
    private static long LOGIN_STATUS_CODE_OUT_TIME = 1 * 60 * 60;
    //登录临时码 持续 10 秒
    private static long LOGIN_UUID_CODE_OUT_TIME = 50;

    /*============================================*/
    /*=================登录过程===================*/
    /*============================================*/

    /**
     * 获取 登录过程
     *
     * @param sessionId
     * @return
     */
    @Override
    public LoginProcessDTO getLoginProcess(String sessionId) {
        ValueOperations<Object, Object> operations = redisTemplate.opsForValue();
        String key = super.buildKey(LOGIN_PROCESS_CODE,sessionId);
        Object val = operations.get(key);
        if (val == null) {
            return null;
        }
        LoginProcessDTO loginProcess = ((JSONObject) val).toJavaObject(LoginProcessDTO.class);
        return loginProcess;
    }

    /**
     * 设置 登录过程
     *
     * @param sessionId
     * @param loginProcess
     */
    @Override
    public void setLoginProcess(String sessionId, LoginProcessDTO loginProcess) {
        ValueOperations<Object, Object> operations = redisTemplate.opsForValue();
        String key = super.buildKey(LOGIN_PROCESS_CODE,sessionId);
        operations.set(key, loginProcess, LOGIN_PROCESS_CODE_OUT_TIME, TimeUnit.SECONDS);
    }

    /**
     * 修改 登录过程
     *
     * @param sessionId
     * @param loginProcess
     */
    @Override
    public void modifyLoginProcess(String sessionId, LoginProcessDTO loginProcess) {
        ValueOperations<Object, Object> operations = redisTemplate.opsForValue();
        String key = super.buildKey(LOGIN_PROCESS_CODE,sessionId);
        redisTemplate.delete(key);
        operations.set(key, loginProcess, LOGIN_PROCESS_CODE_OUT_TIME, TimeUnit.SECONDS);
    }

    /**
     * 移除 登录过程
     *
     * @param sessionId
     */
    @Override
    public void removeLoginProcess(String sessionId) {
        String key = super.buildKey(LOGIN_PROCESS_CODE,sessionId);
        redisTemplate.delete(key);
    }
    /*============================================*/
    /*=================登录状态===================*/
    /*============================================*/
    /**
     * 获取 登录状态
     *
     * @param account
     * @return
     */
    @Override
    public LoginStatusDTO getLoginStatus(String account) {
        ValueOperations<Object, Object> operations = redisTemplate.opsForValue();
        String key = super.buildKey(LOGIN_STATUS_CODE,account);
        Object val = operations.get(key);
        if (val == null) {
            return null;
        }
        LoginStatusDTO dto = ((JSONObject) val).toJavaObject(LoginStatusDTO.class);
        return dto;
    }

    /**
     * 设置 登录状态
     *
     * @param account
     * @param loginStatus
     */
    @Override
    public void setLoginStatus(String account, LoginStatusDTO loginStatus) {
        ValueOperations<Object, Object> operations = redisTemplate.opsForValue();
        String key = super.buildKey(LOGIN_STATUS_CODE, account);
        operations.set(key, loginStatus,LOGIN_STATUS_CODE_OUT_TIME, TimeUnit.SECONDS);
    }

    /**
     * 修改 登录状态
     *
     * @param account
     * @param loginStatus
     */
    @Override
    public void modifyLoginStatus(String account, LoginStatusDTO loginStatus) {
        ValueOperations<Object, Object> operations = redisTemplate.opsForValue();
        String key = super.buildKey(LOGIN_STATUS_CODE, account);
        redisTemplate.delete(key);
        operations.set(key, loginStatus,LOGIN_STATUS_CODE_OUT_TIME, TimeUnit.SECONDS);
    }

    /**
     * 移除 登录状态
     *
     * @param account
     */
    @Override
    public void removeLoginStatus(String account) {
        String key = super.buildKey(LOGIN_STATUS_CODE, account);
        redisTemplate.delete(key);
    }
}
